﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Xfs
{
    [XfsActorMessageHandler(XfsSenceType.XfsClient)]
    public class C4S_VectorActorHandler : XfsAMActorRpcHandler<XfsUnit, C4S_Vector, S4C_Vector>
    {
        protected override async XfsTask Run(XfsUnit unit, C4S_Vector message, Action<S4C_Vector> reply)
        {       
            Vector3 target = new Vector3((float)message.px, 0, (float)message.pz);

            ////Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 22. Id/target: " + unit.Id + "/" + target.ToString());

            this.MoveTo(unit, target).Coroutine();

            //Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + " 23. Id/position: " + unit.Id + "/" + unit.Position.ToString());
        }

        public async XfsVoid MoveTo(XfsUnit unit, Vector3 target)
        {
            unit.GetComponent<XfsMoveComponent>().CancellationTokenSource?.Cancel();
            unit.GetComponent<XfsMoveComponent>().CancellationTokenSource = new CancellationTokenSource();

            await unit.GetComponent<XfsMoveComponent>().MoveToAsync(target, unit.GetComponent<XfsMoveComponent>().CancellationTokenSource.Token);

            unit.GetComponent<XfsMoveComponent>().CancellationTokenSource?.Dispose();
            unit.GetComponent<XfsMoveComponent>().CancellationTokenSource = null;
        }


    }
}
